﻿#ifndef SYSSIGNAL_H
#define SYSSIGNAL_H

#include <QObject>
#include <iostream>
#include <string>
#include <vector>
#include <QObject>
#include "lua.hpp"
#include <QMap>

using namespace std;

typedef enum LUA_STA
{
    LUA_FREE=0,
    LUA_RUN,
    LUA_STOP,
    LUA_WAIT,
    LUA_ERROR,
};
static QList<string> RobZone={
    "fine","z0","z1","z10","z100","z150","z200","z5","z50"
};
static QList<string> RobVcc={
    "v10","v100","v1000","v120","v20","v200","v2000","v5","v50","v60","v80","v800"
};
static QList<string> RobTool={
    "tool0","tool1","shareTool0","shareTool1","shareTool2","shareTool3","shareTool4"
};
static QList<string> RobWobj={
    "wobj0","wobj1","shareWobj0","shareWobj1","shareWobj2","shareWobj3","shareWobj4"
};
static QList<string> RobLoad={
    "load","load0","load1","loadEmpty","loadFull"
};

extern QMap<string,QVector<double> > Jntparamet;
extern QMap<string,QVector<double> > Posparamet;
extern QMap<QString,QVector<double> > mtarget;
extern string mvcc;
extern string mzone;
extern string mtool;
extern string mwobj;
extern string mload;
extern int currntrow;
extern string currentfunction;
extern bool _Next;

static QVector<double> gettablefromelua(lua_State* L,int index);

extern LUA_STA status;
extern QString _mpro;

class SysSignal:public QObject
{
    Q_OBJECT
public:
    SysSignal(QObject* parent=nullptr);
    static SysSignal* instance();
    void targetChanged(QString key,QVector<double> data);
    QVector<double> gettarget(QString key);
    void setexecuteline(int line);
    void setcurrentfunc(string func);
signals:
    void sig_movej();
    void sig_movep();
    void sig_moveL();
    void sig_moveC();
    void sig_runrowchanged(int line);
    void sig_runfuncchanged(QString funcname);
    void sig_stringMsg(QString str);
private:
    void defglobal();
    QString pptomain(QString pro);
    void luainit();
    bool load_file(string path);
public slots:
    void execute();
    void load_string();
public:
    static int MoveAbsJ(lua_State *L);
    static int MoveL(lua_State* L);
    static int MoveC(lua_State* L);
    static int MoveP(lua_State* L);
    static void hook(lua_State* L,lua_Debug* ar);
    static void waitforNext();
private:
    static SysSignal* sys_signal;
    lua_State* Lu;
    string mpro;
};

#endif // SYSSIGNAL_H
